﻿// <copyright file="DynamicArrayExceptionsTests.cs" company="Sinbadsoft">
//        Copyright (c) Sinbadsoft 2009.
//        This file is released under the terms of the
//        GNU Library General Public License (LGPL) version 2.1
//        Please refer to the "License.txt" accompanying this file.
// </copyright>
// <author>Chaker NAKHLI</author>
// <email>Chaker.Nakhli@Sinbadsoft.com</email>
// <date>2009/03/01</date>

namespace Sinbadsoft.AdvancedCollections.Tests.DynamicArray
{
    using System;
    using System.Collections;
    using MbUnit.Framework;

    [TestFixture]
    public static class DynamicArrayExceptionsTests
    {
        [Test]
        public static void ExceptionOnCopyToOfMultiDim()
        {
            Assert.Throws<RankException>(
                delegate
                {
                    DynamicArray<object> dynArray = new DynamicArray<object>(2);
                     object[] copyActual = new object[dynArray.Count];
                    ((ICollection)dynArray).CopyTo(copyActual, 0);
                });
        }
        
        [Test]
        public static void ExceptionOnTooLargeRank()
        {
            Assert.Throws<TypeLoadException>(
                delegate
                {
                    new DynamicArray<object>(121);
                });
        }

        [Test]
        public static void ExceptionOnIncorrectIndexesSize()
        {
            DynamicArray<int> dynArray = new DynamicArray<int>(3);
            int[] tooLargeIndexes = new int[dynArray.Rank + 1];
            int[] tooSmallIndexes = new int[dynArray.Rank - 1];

            // Indexer get
            Assert.Throws<ArgumentException>(
                delegate
                {
                    int i = dynArray[tooSmallIndexes];
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    int i = dynArray[tooLargeIndexes];
                });

            // Indexer set
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray[tooLargeIndexes] = 10;
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray[tooSmallIndexes] = 10;
                });

            // GetValue
            Assert.Throws<ArgumentException>(
                delegate
                {
                    int i = dynArray.GetValue(tooSmallIndexes);
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    int i = dynArray.GetValue(tooLargeIndexes);
                });

            // SetValue
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.SetValue(10, tooLargeIndexes);
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.SetValue(10, tooSmallIndexes);
                });

            // Insert
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.Insert(new int[,,] { { { 1 }, { 2 } }, { { 1 }, { 2 } } }, 0, tooSmallIndexes);
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.Insert(new int[,,] { { { 1 }, { 2 } }, { { 1 }, { 2 } } }, 0, tooLargeIndexes);
                });

            // Resize
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.Resize(tooLargeIndexes);
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.Resize(tooSmallIndexes);
                });

            // Extend
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.Extend(tooLargeIndexes);
                });
            Assert.Throws<ArgumentException>(
                delegate
                {
                    dynArray.Extend(tooSmallIndexes);
                });
       }

        [Test]
        public static void ExceptionOnInvalidDimension()
        {
            DynamicArray<string> dynArray = new DynamicArray<string>(2);
            
            // ExtendDim
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.ExtendDim(dynArray.Rank + 1, 4);
                });
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.ExtendDim(-1, 4);
                });

            // ExtendDim
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.ResizeDim(dynArray.Rank + 1, 4);
                });
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.ResizeDim(-1, 4);
                });

            // ExtendDim
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.GetCapacity(dynArray.Rank + 1);
                });
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.GetCapacity(-1);
                });

            // ExtendDim
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.GetCount(dynArray.Rank + 1);
                });
            Assert.Throws<IndexOutOfRangeException>(
                delegate
                {
                    dynArray.GetCount(-1);
                });
        }
    }
}